#include <iostream>
using namespace std;
int main()
{
    short table[3][3];
    short blankx,blanky;
    bool ifOpen[9] ={false};
    int duiJiang[19]={10000,36,720,360,80,252,108,72,54,180,72,180,119,36,306,1080,144,1800,3600};
    
    for(int i = 0; i<3; i++)
    {
        for(int j = 0; j<3; j++)
        {
            cin>>table[i][j];
            ifOpen[table[i][j]-1] = true;
            if(table[i][j] == 0)
            {
                blankx = i;
                blanky = j;
            }
        }
    }
    for(int i = 0; i<9;i++)
    {
        if(ifOpen[i] == false)
        {
            table[blankx][blanky] = i+1;
        }
    }
    

    for(int i = 0; i<3; i++)
    {
        int x,y;
        cin>>x>>y;
        cout<<table[x-1][y-1]<<endl;
    }
    int direction;
    cin >> direction;
    int sum;
    switch(direction)
    {
        case 1:
        case 2:
        case 3:
            sum = 0;
            for(int i = 0; i<3; i++)
                sum = sum + table[direction-1][i];
            cout << duiJiang[sum-6];
            break;
        case 4:
        case 5:
        case 6:
            sum = 0;
            for(int i = 0; i<3; i++)
                sum = sum + table[i][direction-4];
            cout << duiJiang[sum -6];
            break;
        case 7:
            sum = table[0][0] + table[1][1] + table[2][2];
            cout << duiJiang[sum-6];
           break;
        case 8:
            sum = table[0][2] + table[1][1] + table[2][0];
            cout << duiJiang[sum -6];
    }
    cout<<endl;
}